﻿using System.Collections.ObjectModel;
using System.Configuration;
using System.IO.Ports;
using System.Windows;
using xpds_ProductMonitor.Modbus;
using xpds_ProductMonitor.Models;

namespace xpds_ProductMonitor.BLL
{
    public class GlobalMonitor
    {
        public static ObservableCollection<StuffOutWorkModel> stuffVMList { get; set; } = new ObservableCollection<StuffOutWorkModel>();

        static bool isRunning = true;
        static Task mainTask = null;
        private static SerialInfo serialInfo;
        private static RTU rtu;
        public static List<EnvironmentModel> environmentModelList;

        public static void Start(Action sucessAction, Action<string> faultAction)
        {
            //初始化串口信息
            var result =  initSerialInfo();
            rtu = RTU.getInstance(result.Data);
            StuffService stuffService = new StuffService();
            mainTask = Task.Run(async () =>
            {
                while (isRunning)
                {
                    //员工缺勤信息
                    var stuffResult = stuffService.getList();
                    if (stuffResult.State)
                    {
                        foreach (var item in stuffResult.Data)
                        {
                            stuffVMList.Add(item);
                        }
                    }
                    else
                    {
                        faultAction(stuffResult.Message);return;
                    }
                    //环境信息
                    try
                    {
                        ushort[] envData = rtu.ReadHoldRegister(1, 0, 7);
                        for (int i = 0; i < 7; i++)
                        {
                            environmentModelList[i].EnvironmentValue = envData[i];
                        }
                        await Task.Delay(1000); //1秒钟更新一次数据
                    }
                    catch (Exception ex)
                    {
                        faultAction("程序无法启动，串口连接初始化失败！请检查设备是否连接正常");
                    }
                }
            });
        }

        public static void Stop()
        {
            isRunning = false;
            mainTask.ConfigureAwait(true);
        }

        private static DataResult<SerialInfo> initSerialInfo()
        {
            DataResult<SerialInfo> result = new DataResult<SerialInfo>();
            
            try
            {
                serialInfo = new SerialInfo();
                serialInfo.PortName = ConfigurationManager.AppSettings["port"].ToString();
                serialInfo.BaudRate = int.Parse(ConfigurationManager.AppSettings["baud"].ToString()); 
                serialInfo.DataBit = int.Parse(ConfigurationManager.AppSettings["data_bit"].ToString());
                serialInfo.Parity = (Parity)Enum.Parse(typeof(StopBits), ConfigurationManager.AppSettings["parity"].ToString(), true);
                serialInfo.StopBits = (StopBits)Enum.Parse(typeof(StopBits), ConfigurationManager.AppSettings["stop_bit"].ToString(), true);

                result.State = true;
                result.Data = serialInfo;
            }
            catch (Exception e)
            {
                result.Message = e.Message;
            }
            return result;
        }
    }
}
